"""
Notes on xml.dom.minidom:

dom.ATTRIBUTE_NODE               dom._get_doctype                 dom.createCDATASection           dom.load
dom.CDATA_SECTION_NODE           dom._get_documentElement         dom.createComment                dom.loadXML
dom.COMMENT_NODE                 dom._get_documentURI             dom.createDocumentFragment       dom.localName
dom.DOCUMENT_FRAGMENT_NODE       dom._get_elem_info               dom.createElement                dom.namespaceURI
dom.DOCUMENT_NODE                dom._get_encoding                dom.createElementNS              dom.nextSibling
dom.DOCUMENT_TYPE_NODE           dom._get_errorHandler            dom.createProcessingInstruction  dom.nodeName
dom.ELEMENT_NODE                 dom._get_firstChild              dom.createTextNode               dom.nodeType
dom.ENTITY_NODE                  dom._get_lastChild               dom.doctype                      dom.nodeValue
dom.ENTITY_REFERENCE_NODE        dom._get_localName               dom.documentElement              dom.normalize
dom.NOTATION_NODE                dom._get_standalone              dom.documentURI                  dom.ownerDocument
dom.PROCESSING_INSTRUCTION_NODE  dom._get_strictErrorChecking     dom.encoding                     dom.parentNode
dom.TEXT_NODE                    dom._get_version                 dom.errorHandler                 dom.prefix
dom.__class__                    dom._id_cache                    dom.firstChild                   dom.previousSibling
dom.__doc__                      dom._id_search_stack             dom.getElementById               dom.removeChild
dom.__init__                     dom._magic_id_count              dom.getElementsByTagName         dom.renameNode
dom.__module__                   dom._set_async                   dom.getElementsByTagNameNS       dom.replaceChild
dom.__nonzero__                  dom.abort                        dom.getInterface                 dom.saveXML
dom._call_user_data_handler      dom.actualEncoding               dom.getUserData                  dom.setUserData
dom._child_node_types            dom.appendChild                  dom.hasChildNodes                dom.standalone
dom._create_entity               dom.async                        dom.implementation               dom.strictErrorChecking
dom._create_notation             dom.attributes                   dom.importNode                   dom.toprettyxml
dom._elem_info                   dom.childNodes                   dom.insertBefore                 dom.toxml
dom._get_actualEncoding          dom.cloneNode                    dom.isSameNode                   dom.unlink
dom._get_async                   dom.createAttribute              dom.isSupported                  dom.version
dom._get_childNodes              dom.createAttributeNS            dom.lastChild                    dom.writexml

if doc.hasChildNodes():
    for node in doc.childNodes:
        doc.childNodes[node].parentNode
        doc.childNodes[node].tagName


>>> doc.childNodes[0].childNodes[0].attributes.items()  # .attributes is a dict.
[('id', 'main')]

>>> doc.childNodes[0].childNodes[0].attributes['id'].value
'main'


>>> doc.childNodes[0].childNodes[0].childNodes[0].childNodes[0].wholeText
'This is a test!'
>>> doc.childNodes[0].childNodes[0].childNodes[0].childNodes[0].nodeType
3
>>> doc.childNodes[0].childNodes[0].childNodes[0].nodeType
1
>>> doc.childNodes[0].childNodes[0].nodeType
1
>>> doc.childNodes[0].nodeType
1
>>> doc.nodeType
9


>>> doc.childNodes[0].childNodes[0].childNodes[0].childNodes[0].hasChildNodes()
False
>>> doc.childNodes[0].childNodes[0].childNodes[0].childNodes[0].nodeValue
'This is a test!'
>>> doc.childNodes[0].childNodes[0].childNodes[0].nodeValue
>>> doc.childNodes[0].childNodes[0].nodeValue

.tagName
.hasChildNodes()
.childNodes[0]
.parentNode

>>> wml.childNodes[0].childNodes[0].childNodes
[<DOM Text node "This is a ...">]

"""


############ web example 1 ###############

import xml.dom.minidom

# Create the minidom document
doc = xml.dom.minidom.Document()

# Create the <wml> base element
wml = doc.createElement("wml")
doc.appendChild(wml)

# Create the main <card> element
maincard = doc.createElement("card")
maincard.setAttribute("id", "main")
wml.appendChild(maincard)

# Create a <p> element
paragraph1 = doc.createElement("p")
maincard.appendChild(paragraph1)

# Give the <p> elemenet some text
ptext = doc.createTextNode("This is a test!")
paragraph1.appendChild(ptext)

# Print our newly created XML
print doc.toprettyxml(indent="  ")


############ web example 2 ###############

document = """\
<slideshow>
<title>Demo slideshow</title>
<slide><title>Slide title</title>
<point>This is a demo</point>
<point>Of a program for processing slides</point>
</slide>

<slide><title>Another demo slide</title>
<point>It is important</point>
<point>To have more than</point>
<point>one slide</point>
</slide>
</slideshow>
"""

dom = xml.dom.minidom.parseString(document)

# converts xml to html:
def getText(nodelist):
    rc = ""
    for node in nodelist:
        if node.nodeType == node.TEXT_NODE:
            rc = rc + node.data
    return rc

def handleSlideshow(slideshow):
    print "<html>"
    handleSlideshowTitle(slideshow.getElementsByTagName("title")[0])
    slides = slideshow.getElementsByTagName("slide")
    handleToc(slides)
    handleSlides(slides)
    print "</html>"

def handleSlides(slides):
    for slide in slides:
        handleSlide(slide)

def handleSlide(slide):
    handleSlideTitle(slide.getElementsByTagName("title")[0])
    handlePoints(slide.getElementsByTagName("point"))

def handleSlideshowTitle(title):
    print "<title>%s</title>" % getText(title.childNodes)

def handleSlideTitle(title):
    print "<h2>%s</h2>" % getText(title.childNodes)

def handlePoints(points):
    print "<ul>"
    for point in points:
        handlePoint(point)
    print "</ul>"

def handlePoint(point):
    print "<li>%s</li>" % getText(point.childNodes)

def handleToc(slides):
    for slide in slides:
        title = slide.getElementsByTagName("title")[0]
        print "<p>%s</p>" % getText(title.childNodes)

handleSlideshow(dom)


############ my stuff ###############

h = \
"""<?xml version="1.0"?>
<catalog name="bosco">
   <address>Gambar</address>
   <book name="bk101">
      <address>Gambardella, Matthew</address>
      <title>XML Developer's Guide</title>
      <genre>g</genre>
   </book>
   <book name="bk102">
      <address>Ralls, Kim</address>
      <title>Midnight Rain</title>
      <?hoob nobbo='val"?>
      <genre>Fantasy</genre>
       <book name="bk103">
          <address>Corets, Eva</address>
          <title>Maeve Ascendant</title>
          <genre>g</genre>
       </book>
   </book>
   <book name="bk2">
      <address>Corets, Eva</address>
      <title>The Sundered Grail</title>
      <genre>Fantasy</genre>
   </book>
</catalog>
"""

dom = xml.dom.minidom.parseString(h)

